package com.badibadi.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.badibadi.activity.ClubHomepageActivity;
import com.badibadi.infos.ClubModel;
import com.badibadi.infos.ClubMy_Model;
import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.Constants;
import com.badibadi.uniclubber.R;
import com.google.zxing.common.BitArray;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 我的俱乐部列表适配器 创建时间2014-8-27
 * 
 * @author Administrator
 * 
 */
public class ClubMyClubListAdapter extends BaseAdapter {
	private Context context;
	private List<ClubMy_Model> clubModels;
	private DisplayImageOptions options;

	public ClubMyClubListAdapter(Context context, List<ClubMy_Model> clubModels) {
		this.context = context;
		this.clubModels = clubModels;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public int getCount() {
		if (clubModels.size() == 0) {
			return 0;
		}
		if (clubModels.size() == 1) {
			return 2;
		} else
			return (clubModels.size() + 1);
	}

	@Override
	public Object getItem(int position) {
		int pos = 0;
		if (position == 0) {
			pos = 0;
			return clubModels.get(pos);
		} else if (position == 1) {
			pos = 0;
			return clubModels.get(pos);
		} else if (position > 1) {
			pos = position - 2;
			return clubModels.get(pos);
		}
		return clubModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0)
			return 0;
		else if (position == 1)
			return 1;
		else
			return 2;

	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
		int type = getItemViewType(position);
		// if (position > 1) {
		if (type == 2) {
			final int pos = position - 1;
			convertView = inflater.inflate(
					R.layout.regionhomepage_import_layout2, null);
			TextView clubOverview_item_OverAddress = (TextView) convertView
					.findViewById(R.id.clubOverview_item_OverAddress);// 发源地
			TextView clubOverview_item_name = (TextView) convertView
					.findViewById(R.id.clubOverview_item_name);// 俱乐部名称
			TextView clubOverview_item_infos = (TextView) convertView
					.findViewById(R.id.clubOverview_item_infos);// 俱乐部介绍
			TextView clubOverview_item_addnum = (TextView) convertView
					.findViewById(R.id.clubOverview_item_addnum);// 会员人数
			TextView clubOverview_item_price = (TextView) convertView
					.findViewById(R.id.clubOverview_item_price);// 加入所需金额
			ImageView clubOverview_item_img = (ImageView) convertView
					.findViewById(R.id.clubOverview_item_img);// 俱乐部背景
			clubOverview_item_addnum.setText(clubModels.get(pos).getAddnum());
			clubOverview_item_infos.setText(clubModels.get(pos).getInfo());
			clubOverview_item_name.setText(clubModels.get(pos).getName());
			clubOverview_item_OverAddress.setText(clubModels.get(pos)
					.getOriginate_address());
			clubOverview_item_price.setText(clubModels.get(pos).getPrice());
			// ImageLoader.getInstance().displayImage(
			// Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
			// + Constants.App9block, clubOverview_item_img,
			// options);
			new BitmapHelp().displayImage(context, clubOverview_item_img,
					Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
							+ Constants.App9block);
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(context,
							ClubHomepageActivity.class);
					intent.putExtra("cid", clubModels.get(pos).getId());
					intent.putExtra("c_uid", clubModels.get(pos).getUid());
					intent.putExtra("is_join", clubModels.get(pos)
							.getIs_joined());
					intent.putExtra("logo", clubModels.get(pos).getLogo());
					context.startActivity(intent);
				}
			});
		} else if (type == 0) {
			// if (position == 0) {
			final int pos = 0;
			convertView = inflater.inflate(
					R.layout.regionhomepage_import_layout2_1, null);
			TextView clubOverview_item_OverAddress = (TextView) convertView
					.findViewById(R.id.clubOverview_item_OverAddress1);// 发源地
			ImageView clubOverview_item_img = (ImageView) convertView
					.findViewById(R.id.clubOverview_item_img1);// 俱乐部背景(wancheng)
			clubOverview_item_OverAddress.setText(clubModels.get(pos)
					.getOriginate_address());// originate_address
//			ImageLoader.getInstance().displayImage(
//					Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
//							+ Constants.App9block, clubOverview_item_img,
//					options);
			new BitmapHelp().displayImage(context, clubOverview_item_img,
					Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
							+ Constants.App9block);
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(context,
							ClubHomepageActivity.class);
					intent.putExtra("cid", clubModels.get(pos).getId());
					intent.putExtra("c_uid", clubModels.get(pos).getUid());
					intent.putExtra("is_join", clubModels.get(pos)
							.getIs_joined());
					intent.putExtra("logo", clubModels.get(pos).getLogo());
					context.startActivity(intent);
				}
			});
		} else if (type == 1) {
			final int pos = 0;
			convertView = inflater.inflate(
					R.layout.regionhomepage_import_layout2_2, null);
			TextView clubOverview_item_name = (TextView) convertView
					.findViewById(R.id.clubOverview_item_name1);// 俱乐部名称
			TextView clubOverview_item_infos = (TextView) convertView
					.findViewById(R.id.clubOverview_item_infos1);// 俱乐部介绍
			TextView clubOverview_item_addnum = (TextView) convertView
					.findViewById(R.id.clubOverview_item_addnum1);// 会员人数
			TextView clubOverview_item_price = (TextView) convertView
					.findViewById(R.id.clubOverview_item_price1);// 加入所需金额
			clubOverview_item_addnum.setText(clubModels.get(pos).getAddnum());
			clubOverview_item_infos.setText(clubModels.get(pos).getInfo());
			clubOverview_item_name.setText(clubModels.get(pos).getName());
			clubOverview_item_price.setText(clubModels.get(pos).getPrice());
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(context,
							ClubHomepageActivity.class);
					intent.putExtra("cid", clubModels.get(pos).getId());
					intent.putExtra("c_uid", clubModels.get(pos).getUid());
					intent.putExtra("is_join", clubModels.get(pos)
							.getIs_joined());
					intent.putExtra("logo", clubModels.get(pos).getLogo());
					context.startActivity(intent);
				}
			});
		}

		return convertView;
	}
}
